<template>
<div :class="[$style.container]" @keyup.enter="onConfirm">
<el-dialog title="客户端设置" :visible.sync="actualVisible" width="480px"
  :close-on-click-modal="false" :close-on-press-escape="false">
  <el-form size="small" label-position="top" :rules="rules" :model="form" @submit.native.prevent="" ref="fm">
    <el-form-item prop="api" label="文档服务地址">
      <el-input v-model="form.api" placeholder="例如：http://127.0.0.1:8001"></el-input>
    </el-form-item>
    <el-form-item prop="previewUrl" label="预览服务地址">
      <el-input v-model="form.previewUrl" placeholder="例如：http://127.0.0.1:8003"></el-input>
    </el-form-item>
  </el-form>
  <span slot="footer">
    <el-button size="small" @click="onCancel" class="px-5" :disabled="submitting">取 消</el-button>
    <el-button size="small" type="primary" @click="onConfirm" :loading="submitting" class="px-5">确 定</el-button>
  </span>
</el-dialog>
</div>
</template>

<script lang="ts">

import { Vue, Component, Prop, Emit, Ref } from 'vue-property-decorator'
import { ModalMixins } from '@/mixins'
import { mixins } from 'vue-class-component'
import { getInitOptions, setInitOptions } from '@/config/init'

@Component
export default class ConfigModal extends mixins(ModalMixins) {
  @Ref() fm!: any

  data: any = null

  row: any = null

  form = {
    api: '',
    previewUrl: ''
  }

  rules = {
    api: [
      { required: true, message: '请输入文档服务地址', trigger: 'change' }
    ],
    previewUrl: [
      { required: true, message: '请输入预览服务地址', trigger: 'change' }
    ]
  }

  get url () {
    return getInitOptions().api + '/s/' + this.row.id
  }

  get isEdit () {
    return !!this.row
  }

  open () {
    this.$nextTick(() => {
      if (this.fm) this.fm.resetFields()
      this.initForm()
    })
    return this.init()
  }

  initForm () {
    const c = getInitOptions()
    this.form = {
      api: c?.api || '',
      previewUrl: c?.previewUrl || ''
    }
  }

  onCancel () {
    this.cancel()
  }

  onConfirm () {
    this.fm.validate().then((valid: boolean) => {
      if (!valid) return
      localStorage.removeItem('jwt_token')
      localStorage.removeItem('token')
      setInitOptions(this.form)
      this.complete()
      this.refresh()
    })
  }

  refresh () {
    this.$http.inst.defaults.baseURL = getInitOptions().api
    this.$router.push('/login')
    window.location.reload()
  }
}
</script>

<style lang="scss" module>
@import "~@/styles/vars.scss";

.container {

}

.detailWrap {
  display: flex;
  align-items: center;

  & > div:first-child {
    flex:  1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  & > div:nth-child(2) {
    flex: 0 0 auto;
  }

  & > div:last-child {
    flex: 0 0 auto;
  }
}

.createWrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.text {
  font-size: 12px;
  margin-right: 8px;
  color: $text-secondary-color;
}

</style>
